package phoneregistration.service.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import phoneregistration.service.BaseService;

public abstract class AbstractService<M, ID extends Serializable> implements BaseService<M, ID> {

    protected CrudRepository<M, ID> repository;

    public AbstractService(CrudRepository<M, ID> repository) {
        this.repository = repository;
    }

    public Set<M> findAll() {
        Set<M> set = new HashSet<M>();
        final Iterable<M> entities = repository.findAll();
        if (entities != null) {
            for (M model : entities) {
                set.add(model);
            }
        }
        return set;
    }

    public M findOne(ID id) {
        return repository.findOne(id);
    }

    @Transactional
    public M save(M model) {
        return repository.save(model);
    }

    @Transactional
    public void delete(ID id) {
        repository.delete(id);
    }
}
